package com.wetao.lib_common.common.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.wetao.lib_common.base.Base2Response;
import com.wetao.lib_common.base.BaseResponse;
import com.wetao.lib_common.common.contract.PwdResetContract;
import com.wetao.lib_common.net.RetryWithDelay;
import com.wetao.lib_common.net.RxUtils;
import com.wetao.lib_common.net.ServerUtils;
import com.wetao.lib_common.net.callback.RequestCallback;
import com.wetao.lib_common.net.callback.RxErrorHandler;
import com.wetao.lib_common.utils.ToastUtil;

import java.util.HashMap;

import okhttp3.RequestBody;

/**
 * 登录 P
 */
public class PwdResetPresenter extends PwdResetContract.Presenter {

    private Context context;

    public PwdResetPresenter(Context context) {
        this.context = context;
    }


    @Override
    public void getResetPwd(HashMap<String, String> map, boolean isDialog, boolean cancelable) {
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().resetNewPwd(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<Base2Response>bindToLifecycle(getView()))
                .compose(RxUtils.<Base2Response>getSchedulerTransformer())
                .subscribe(new RequestCallback<Base2Response>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(Base2Response baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getResetPwdResult(baseResponse);
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }


    @Override
    public void getVerifyCode(HashMap<String, String> map, boolean isDialog, boolean cancelable) {
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().getVerifyCode(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse>(context, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getVerifyCodeResult(baseResponse);
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

}
